﻿using System.Linq.PageList;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Dyao.Contract.Model;
using Foundation.Core;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class CommonController : ApiV1BaseController
    {
        private readonly ICommonApplication _commonApplication;

        public CommonController(ICommonApplication commonApplication)
        {
            _commonApplication = commonApplication;
        }

        /// <summary>
        /// 获取选项集合
        /// </summary>
        /// <param name="key">选项key</param>
        /// <param name="category">选项类型</param>
        /// <param name="arguments">参数</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost("getSelectByQuery")]
        public async Task<ApiResult<SelectCollectionQueryResultItem>> GetSelectCollectionAsync([FromQuery] string key, [FromQuery] SelectCategory category, [FromBody] IDictionary<string, object> arguments = null, CancellationToken cancellationToken = default)
        {
            var service = this.ServiceProvider.GetRequiredService<ISelectCollectionService>();

            var dict = arguments?.ToDictionary(a => a.Key, a =>
            {
                if (a.Value is Newtonsoft.Json.Linq.JToken jtoken) return jtoken.ToString(Newtonsoft.Json.Formatting.None);

                return (object)a.Value?.ToString();
            }, StringComparer.OrdinalIgnoreCase);

            var collection = await service.GetCollctionAsync(key, category, dict, cancellationToken);

            var result = collection == null ? null : new SelectCollectionQueryResultItem { Key = key, Category = category, Items = collection.Items };

            return result;
        }

        /// <summary>
        /// 获取选项集合
        /// </summary>
        /// <param name="items">选项查询参数</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<List<SelectCollectionQueryResultItem>>> GetSelectCollectionAsync(List<SelectCollectionQueryItem> items, CancellationToken cancellationToken)
        {
            var result = new List<SelectCollectionQueryResultItem>(items.Count);

            if (items.Count > 0)
            {
                var service = this.ServiceProvider.GetRequiredService<ISelectCollectionService>();

                items = items.GroupBy(a => new { a.Key, a.Category }).Select(g => g.First()).ToList(); //去重

                foreach (var item in items)
                {
                    var collection = await service.GetCollctionAsync(item.Key, item.Category, null, cancellationToken);

                    if (collection != null)
                    {
                        result.Add(new SelectCollectionQueryResultItem { Key = item.Key, Category = item.Category, Items = collection.Items });
                    }
                }
            }

            return result;
        }

        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<OperationLogResultDto>>> GetOpLogPageListAsync(OperationLogFilterDto filterModel, CancellationToken cancellationToken = default)
        {
            var filter = filterModel;
            var pageFilter = filter.ToPageFilter(filter);
            return (PageList<OperationLogResultDto>)await _commonApplication.GetOpLogPageListAsync(pageFilter, cancellationToken);
        }
    }
}
